package top.resty.erp.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.resty.erp.domain.Storage;
import top.resty.erp.service.StorageService;
import top.resty.erp.util.LayuiJson;

import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author : resty
 * @date : 2021年03月31 17:56
 */
@Controller
@RequestMapping("/storage")
public class StorageController {
	
	@Autowired
	private StorageService storageService;
	
	
	@RequestMapping("/getStandardsName")
	@ResponseBody
	public List<String> getStandardsName(String type){
		System.out.println("storageService.getStandardsName(type) = " + storageService.getStandardsName(type));
		return storageService.getStandardsName(type);
	}
	
	@RequestMapping("/getTypesName")
	@ResponseBody
	public List<String> getTypesName(String variety){
		return storageService.getTypesName(variety);
	}
	
	
	@RequestMapping("/findStorageByCondition")
	@ResponseBody
	public LayuiJson<Storage> findStorageByvarietyName(String variety,String type,String standard,int page,int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		Storage storage = new Storage();
		storage.setVariety(variety);
		storage.setType(type);
		storage.setStandard(standard);
		List<Storage> list = storageService.findStorageByCondition(storage);
		
		LayuiJson<Storage> layuiJson = new LayuiJson<Storage>();
		layuiJson.setData(list);
		layuiJson.setCount((int) pages.getTotal());
		
		return layuiJson;
	}
	
	@RequestMapping("/allList")
	@ResponseBody
	public LayuiJson<Storage> findAllStorage(int page, int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		
		List<Storage> storages = storageService.findAll();
		
		LayuiJson layuiJson = new LayuiJson();
		layuiJson.setCount((int) pages.getTotal());
		layuiJson.setData(storages);
		
		return layuiJson;
	}
	
	
	@RequestMapping("/list")
	public String goToStoragePage(Model model){
		
		model.addAttribute("varieties",storageService.findByVarieties());
		
		return "man/storage";
	}
}
